
function chooseUser(params){
    var options = {    
                    url: baseURL + '/orgUser/tree',    
                    param: {orgId: params.orgid},    
                    userName: params.valname,    
                    idName: params.idname,
                    onlyLeafSelect: true,
                    event:"none",
                    confirm: function(param) {
                        $("#"+params.valOfid).val(param.text);
                        $("#"+params.idOfid).val(param.id);
                        if(params.callback){
                            params.callback($("#"+params.idOfid));
                        }
                    }
                }; 
    $("#"+params.valOfid).outree(options);
}

function changeRequired(v){
	if(v == '0'){
		$("#shortlistExpireDate_"+productCount).datebox({required:false});
	}else if(v == '1'){
		$("#shortlistExpireDate_"+productCount).datebox({required:true});
	}
}

/**
 * 创建table
 */
function tableUtils(){
    this.index=0;
    this.setIndex=function(v){this.index=v;};
    this.tds=[];
    this.clear=function(){
        this.tds.length=0;
        this.index=0;
        this.tds=[];
    };

    this.addHidden=function(name,val){
        this.addTD(name,"hidden",val);
    };

    this.addInput=function(name,val){
        this.addTD(name,"input",val);
    };
    
    this.addTextarea =function(name,val){
        this.addTD(name,"textarea",val);
    };
    
    this.addInputSet=function(name,val,width){
        this.addTDSet(name,"inputSet",val,width);
    };
    
    this.addInputRead=function(name,val,width){
        this.addTDSet(name,"inputRead",val,width);
    };
    
    this.addCheckbox=function(name,val){
        this.addTD(name,"checkbox",val);
    };

    this.addText=function(name,val){
        this.addTD(name,"text",val);
    };
    
    this.addStr=function(val){
        this.tds[this.tds.length]={
            type:"str",
            val:val
        };
    };
    
    this.addEventInput=function(name,val,eventname,event){
        this.tds[this.tds.length]={
            type:"input",
            name:name,
            val:val,
            eventname:eventname,
            event:event
        };
    };
    this.addEventText=function(name,val,eventname,event){
        this.tds[this.tds.length]={
            type:"text",
            name:name,
            val:val,
            eventname:eventname,
            event:event
        };
    };

    this.addTD=function(name,type,val){
        this.tds[this.tds.length]={
            type:type,
            name:name,
            val:val
        };
    };
    
    this.addTDSet=function(name,type,val,width){
        this.tds[this.tds.length]={
            type:type,
            name:name,
            val:val,
            width:width
        };
    };
    
    this.addSelect=function(name,val,text){
        this.tds[this.tds.length]={
            type:"select",
            name:name,
            val:val,
            text:text
        };
    };
    
    this.addMGSelect=function(name,val,text){
        this.tds[this.tds.length]={
            type:"addMGSelect",
            name:name,
            val:val,
            text:text
        };
    };
    
    this.addOnchangeSelect=function(name,val,text){
        this.tds[this.tds.length]={
            type:"addOnchangeSelect",
            name:name,
            val:val,
            text:text
        };
    };
    
    this.ahSelect=function(name,val,text){
        this.tds[this.tds.length]={
            type:"ahSelect",
            name:name,
            val:val,
            text:text
        };
    };
    
    this.jsonSelect=function(name,data){
        this.tds[this.tds.length]={
            type:"jsonSelect",
            name:name,
            data:data
        };
    };
    
    this.addPerSel=function(idname,valname,width,height,orgid,callback){
        this.tds[this.tds.length]={
            type:"persel",
            idname:idname,
            valname:valname,
            width:width||200,
            height:height||20,
            orgid:orgid||"root",
            callback:callback
        };
    };
    
    this.toString=function(){
        var str="<tr>";
        var ind=this.index;
        for(var i=0;i<this.tds.length;i++){
            var item=this.tds[i];
            var t=item.type;
            var e="";
            if(item.eventname){
                    e=item.eventname+"='"+item.event+"' ";
            }
            
            if(t=="input" && item.name=="b__remark"){
                str+="<td><input type='text' maxlength='150' class='textinput' "+e+" id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"' /></td>";
            }else if(t=="input"){
                str+="<td><input type='text' class='textinput' "+e+" id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"'/></td>";
            }else if(t=="checkbox"){
                str+="<td align='center' class='xm_formtablefixradio'><input type='checkbox' id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"'/></td>";
            }else if(t=="text"){
                str+="<td><span "+e+" id='"+item.name+"_"+ind+"'>"+item.val+"</span></td>";
            }else if(t=="hidden"){
                str+="<input type='hidden' id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"'/>";
            }else if(t=="str"){
                str+=item.val;
            }else if(t=="select"){
                str+="<td><select id='"+item.name+"_"+ind+"' name='"+item.name+"' "+e+" >";
                for(var j=0;j<item.val.length;j++){
                    str+="<option value='"+item.val[j]+"'>"+item.text[j]+"</option>";
                }
                str+="</select></td>";
            }else if(t=="persel"){
                var valOfid = item.valname+"_"+ind;
                var idOfid = item.idname+"_"+ind;
                str+="<td><input id='"+valOfid+"' name='"+item.valname+"' style='width:"+item.width+"; height: "+item.height+"; cursor: pointer;'";
                str+=" type='text' readOnly='readonly' onclick=\"chooseUser({valname:'"+item.valname+"',valOfid:'"+valOfid+"',idname:'"+item.idname+"',idOfid:'"+idOfid+"',orgid:'"+item.orgid+"'";
                if(item.callback)
                    str+=",callback:"+item.callback+"})\"/>";
                else
                    str+="})\"/>";
                str+="<input id='"+idOfid+"' name='"+item.idname+"' type='hidden'/></td>";
            }else if(t == "inputSet"){
                str+="<td><input type='text' style='width:"+item.width+"' id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"'/></td>";
            }else if(t == "inputRead"){
                str+="<td><input type='text' style='width:"+item.width+"' id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"' readonly='readonly'/></td>";
            }else if(t == "addMGSelect"){
            	str+="<td><div class='xm_select xm_select4' style='width:90%;'><select class='xm_selectin' id='"+item.name+"_"+ind+"' name='"+item.name+"' "+e+" >";
                for(var j=0;j<item.val.length;j++){
                    str+="<option value='"+item.val[j]+"'>"+item.text[j]+"</option>";
                }
                str+="</select></div></td>";
            }else if(t == "addOnchangeSelect"){
            	str+="<td><select required id='"+item.name+"_"+ind+"' name='"+item.name+"' onchange='changeRequired(this.options[this.options.selectedIndex].value)' >";
                for(var j=0;j<item.val.length;j++){
                    str+="<option value='"+item.val[j]+"'>"+item.text[j]+"</option>";
                }
                str+="</select></td>";
            }else if(t=="ahSelect"){
                str+="<td><div class='xm_select xm_select4' style='width:80%'><select class='xm_selectin' id='"+item.name+"_"+ind+"' name='"+item.name+"' "+e+" >";
                for(var j=0;j<item.val.length;j++){
                    str+="<option value='"+item.val[j]+"'>"+item.text[j]+"</option>";
                }
                str+="</select></div></td>";
            }else if(t=="jsonSelect"){
                str+="<td><div class='xm_select xm_select4' style='width:80%'><select class='xm_selectin' id='"+item.name+"_"+ind+"' name='"+item.name+"' "+e+" >";
                for(var j=0;j<item.data.length;j++){
                    str+="<option value='"+item.data[j].code+"'>"+item.data[j].name+"</option>";
                }
                str+="</select></div></td>";
            }else if(t=="textarea"){
                str+="<td>" +
                		"<textarea name='"+item.name+"' cols='30' rows='3' id='"+item.name+"_"+ind+"'>"+item.val+"</textarea>" +
                		"</td>";
            }
            
        }
        str+="</tr>";
        return str;
    };
    
    this.changeToString=function(){
        var str="<tr style='text-align:center'>";
        var ind=this.index;
        for(var i=0;i<this.tds.length;i++){
            var item=this.tds[i];
            var t=item.type;
            var e="";
            if(item.eventname){
                    e=item.eventname+"='"+item.event+"' ";
            }
            
            if(t=="input" && item.name=="b__remark"){
                str+="<td><input type='text' maxlength='150' class='textinput' "+e+" id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"' /></td>";
            }else if(t=="input"){
                str+="<td><input type='text' class='textinput' "+e+" id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"'/></td>";
            }else if(t=="checkbox"){
                str+="<td align='center' class='xm_formtablefixradio'><input type='checkbox' id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"'/></td>";
            }else if(t=="text"){
                str+="<td><span "+e+" id='"+item.name+"_"+ind+"'>"+item.val+"</span></td>";
            }else if(t=="hidden"){
                str+="<input type='hidden' id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"'/>";
            }else if(t=="str"){
                str+=item.val;
            }else if(t=="select"){
                str+="<td><select id='"+item.name+"_"+ind+"' name='"+item.name+"' "+e+" >";
                for(var j=0;j<item.val.length;j++){
                    str+="<option value='"+item.val[j]+"'>"+item.text[j]+"</option>";
                }
                str+="</select></td>";
            }else if(t=="persel"){
                var valOfid = item.valname+"_"+ind;
                var idOfid = item.idname+"_"+ind;
                str+="<td><input id='"+valOfid+"' name='"+item.valname+"' style='width:"+item.width+"; height: "+item.height+"; cursor: pointer;'";
                str+=" type='text' readOnly='readonly' onclick=\"chooseUser({valname:'"+item.valname+"',valOfid:'"+valOfid+"',idname:'"+item.idname+"',idOfid:'"+idOfid+"',orgid:'"+item.orgid+"'";
                if(item.callback)
                    str+=",callback:"+item.callback+"})\"/>";
                else
                    str+="})\"/>";
                str+="<input id='"+idOfid+"' name='"+item.idname+"' type='hidden'/></td>";
            }else if(t == "inputSet"){
                str+="<td><input type='text' style='width:"+item.width+"' id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"'/></td>";
            }else if(t == "inputRead"){
                str+="<td><input type='text' style='width:"+item.width+"' id='"+item.name+"_"+ind+"' name='"+item.name+"' value='"+item.val+"' readonly='readonly'/></td>";
            }else if(t == "addMGSelect"){
            	str+="<td><div class='xm_select xm_select4' style='width:90%;'><select class='xm_selectin' id='"+item.name+"_"+ind+"' name='"+item.name+"' "+e+" >";
                for(var j=0;j<item.val.length;j++){
                    str+="<option value='"+item.val[j]+"'>"+item.text[j]+"</option>";
                }
                str+="</select></div></td>";
            }else if(t == "addOnchangeSelect"){
            	str+="<td class='shortlist_select'><select id='"+item.name+"_"+ind+"' name='"+item.name+"' onchange='changeRequired(this.options[this.options.selectedIndex].value)' >";
                for(var j=0;j<item.val.length;j++){
                    str+="<option value='"+item.val[j]+"'>"+item.text[j]+"</option>";
                }
                str+="</select></td>";
            }else if(t=="ahSelect"){
                str+="<td><div class='xm_select xm_select4' style='width:80%'><select class='xm_selectin' id='"+item.name+"_"+ind+"' name='"+item.name+"' "+e+" >";
                for(var j=0;j<item.val.length;j++){
                    str+="<option value='"+item.val[j]+"'>"+item.text[j]+"</option>";
                }
                str+="</select></div></td>";
            }else if(t=="jsonSelect"){
                str+="<td><div class='xm_select xm_select4' style='width:80%'><select class='xm_selectin' id='"+item.name+"_"+ind+"' name='"+item.name+"' "+e+" >";
                for(var j=0;j<item.data.length;j++){
                    str+="<option value='"+item.data[j].code+"'>"+item.data[j].name+"</option>";
                }
                str+="</select></div></td>";
            }else if(t=="textarea"){
                str+="<td>" +
        		"<textarea name='"+item.name+"' cols='' rows='3' id='"+item.name+"_"+ind+"'>"+item.val+"</textarea>" +
        		"</td>";
            }
        }
        str+="</tr>";
        return str;
    };
}


//加法函数  
function accAdd(arg1, arg2) {  
    var r1, r2, m;  
    try {  
        r1 = arg1.toString().split(".")[1].length;  
    }  
    catch (e) {  
        r1 = 0;  
    }  
    try {  
        r2 = arg2.toString().split(".")[1].length;  
    }  
    catch (e) {  
        r2 = 0;  
    }  
    m = Math.pow(10, Math.max(r1, r2));  
    return (arg1 * m + arg2 * m) / m;  
}

//给Number类型增加一个add方法，，使用时直接用 .add 即可完成计算。   
Number.prototype.add = function (arg) {  
    return accAdd(arg, this);  
};

//减法函数  
function Subtr(arg1, arg2) {  
    var r1, r2, m, n;  
    try {  
        r1 = arg1.toString().split(".")[1].length;  
    }  
    catch (e) {  
        r1 = 0;  
    }  
    try {  
        r2 = arg2.toString().split(".")[1].length;  
    }  
    catch (e) {  
        r2 = 0;  
    }  
    m = Math.pow(10, Math.max(r1, r2));  
     //last modify by deeka  
     //动态控制精度长度  
    n = (r1 >= r2) ? r1 : r2;  
    return ((arg1 * m - arg2 * m) / m).toFixed(n);  
}

//给Number类型增加一个add方法，，使用时直接用 .sub 即可完成计算。   
Number.prototype.sub = function (arg) {  
    return Subtr(this, arg);  
};  
  
//乘法函数  
function accMul(arg1, arg2) {  
    var m = 0, s1 = arg1.toString(), s2 = arg2.toString();  
    try {  
        m += s1.split(".")[1].length;  
    }  
    catch (e) {  
    }  
    try {  
        m += s2.split(".")[1].length;  
    }  
    catch (e) {  
    }  
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m);  
}

//给Number类型增加一个mul方法，使用时直接用 .mul 即可完成计算。   
Number.prototype.mul = function (arg) {  
    return accMul(arg, this);  
};   
  
//除法函数  
function accDiv(arg1, arg2) {  
    var t1 = 0, t2 = 0, r1, r2;  
    try {  
        t1 = arg1.toString().split(".")[1].length;  
    }  
    catch (e) {  
    }  
    try {  
        t2 = arg2.toString().split(".")[1].length;  
    }  
    catch (e) {  
    }  
    with (Math) {  
        r1 = Number(arg1.toString().replace(".", ""));  
        r2 = Number(arg2.toString().replace(".", ""));  
        return (r1 / r2) * pow(10, t2 - t1);  
    }  
} 

//给Number类型增加一个div方法，，使用时直接用 .div 即可完成计算。   
Number.prototype.div = function (arg) {  
    return accDiv(this, arg);  
};